package cn.ynu.cmrs.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;
import cn.ynu.cmrs.R;

/**
 * Play video according to video length
 * 
 * @author Yasha
 * 
 */
public class VideoUtil {

	public static void playVideoFromUrl(final String[] video,
			final Activity mContext) {
		int videoNumber;
		if (video == null)
			videoNumber = -1;
		else
			videoNumber = video.length;
		switch (videoNumber) {
		case -1:
		case 0:
			Toast.makeText(mContext, R.string.no_video, Toast.LENGTH_LONG)
					.show();
			break;
		case 1:
			/*Intent it = new Intent(mContext, VideoPlayerActivity.class)
					.putExtra("videoUrl", video[0]);
			mContext.startActivityForResult(it, 109);*/
			Log.i("VideoUrl", video[0]);
			Intent intent = new Intent();
			Uri uri = Uri.parse(video[0]);
			intent.setAction(Intent.ACTION_VIEW);
			intent.setData(uri);
			mContext.startActivity(intent);
			break;
		default:// Show a dialog to select video chapter
			String[] items = new String[video.length];
			for (int i = 1; i <= video.length; i++) {
				items[i - 1] = "Chapter" + i;
			}
			new AlertDialog.Builder(mContext).setTitle(R.string.select_chapter)
					.setCancelable(true)
					.setItems(items, new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							/*Intent it = new Intent(mContext,
									VideoPlayerActivity.class).putExtra(
									"videoUrl", video[which]);
							mContext.startActivityForResult(it, 109);*/
							Log.i("VideoUrl", video[which]);
							Intent intent = new Intent();
							Uri uri = Uri.parse(video[which]);
							intent.setAction(Intent.ACTION_VIEW);
							intent.setData(uri);
							mContext.startActivity(intent);
						}
					}).show();
			break;
		}
	}
}
